package com.eol.admin.modules.business.content;

import com.eol.admin.modules.logging.log.Log;
import com.eol.common.exception.BaseException;
import com.eol.common.sequence.SequenceGen;
import com.eol.common.utils.StringUtils;
import com.eol.entity.po.content.ZCourseType;
import com.eol.service.content.ZCourseTypeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * ZCourseTypeController
 *
 * @author liulinhui
 * @email 1249055292@qq.com
 * @date 2020/6/9 10:56
 */
@RestController
@RequestMapping("api/courseType")
public class ZCourseTypeController {

    @Resource
    private ZCourseTypeService zCourseTypeService;
    @Resource
    private SequenceGen sequenceGen;

    @GetMapping
    public ResponseEntity<Object> list(@RequestParam(value = "name", required = false) String name) {
        return new ResponseEntity<>(zCourseTypeService.buildTree(
                zCourseTypeService.selectCourseList(ZCourseType.builder().name(name).build())),
                HttpStatus.OK);
    }

    @Log("新增商户")
    @PreAuthorize("@el.check('courseType:add')")
    @PostMapping
    public ResponseEntity<Object> add(@Validated @RequestBody ZCourseType zCourseType) {
        zCourseType.setId(sequenceGen.genCourseTypeId());
        zCourseType.setCreateTime(new Date());
        validateType(zCourseType);
        return new ResponseEntity<>(zCourseTypeService.insertSelective(zCourseType), HttpStatus.OK);
    }

    @Log("编辑商户")
    @PutMapping
    @PreAuthorize("@el.check('courseType:edit')")
    public ResponseEntity<Object> edit(@Validated @RequestBody ZCourseType zCourseType) {
        validateType(zCourseType);
        return new ResponseEntity<>(zCourseTypeService.updateSelectiveById(zCourseType), HttpStatus.OK);
    }

    @Log("删除商户")
    @DeleteMapping("/{id}")
    @PreAuthorize("@el.check('courseType:del')")
    public ResponseEntity<Object> del(@PathVariable("id") String id) {
        //判断是否存在下级分类
        long b = zCourseTypeService.selectCount(ZCourseType.builder()
                .pid(id)
                .build());
        if (b > 0)
            throw new BaseException("该分类下存在子分类，不可以删除");
        return new ResponseEntity<>(zCourseTypeService.deleteById(id), HttpStatus.OK);
    }


    private void validateType(ZCourseType zCourseType) {
        if (zCourseType.getId().equalsIgnoreCase(zCourseType.getPid())) {
            throw new BaseException("上级分类不能是自己");
        }
        if (StringUtils.isNotBlank(zCourseType.getPid())) {
            ZCourseType belongto = zCourseTypeService.selectById(zCourseType.getPid());
            if (null == belongto) throw new BaseException("分类不存在");
            if (StringUtils.isNotBlank(belongto.getPid()))
                throw new BaseException("暂不支持三级分类");
        }
    }
}
